﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class List12BlockLevel : List12RecordItem
    {
        private XLUnicodeString _stHeader;
        private int istnAgg = -1;
        private int istnData = -1;
        private int istnHeader = -1;
        private XLUnicodeString stData;
        private XLUnicodeString stAgg;

        public List12BlockLevel(Table table)
        {
        }

        public List12BlockLevel()
        {
        }

        public override List12RecordItemType Type
        {
            get { return List12RecordItemType.List12BlockLevel; }
        }

        public DXFN12List DxfHeader { get; private set; }

        public DXFN12List DxfData { get; private set; }

        public DXFN12List DxfBorder { get; private set; }

        public DXFN12List DxfAgg { get; private set; }

        public DXFN12List DxfHeaderBorder { get; private set; }

        public DXFN12List DxfAggBorder { get; private set; }

        public override void Read(BinaryReader reader)
        {
            int cbdxfHeader = reader.ReadInt32();
            istnHeader = reader.ReadInt32();
            int cbdxfData = reader.ReadInt32();
            istnData = reader.ReadInt32();
            int cbdxfAgg = reader.ReadInt32();
            istnAgg = reader.ReadInt32();
            int cbdxfBorder = reader.ReadInt32();
            int cbdxfHeaderBorder = reader.ReadInt32();
            int cbdxfAggBorder = reader.ReadInt32();
            if (cbdxfHeader != 0)
            {
                DxfHeader = new DXFN12List(reader, cbdxfHeader);
            }
            if (cbdxfData != 0)
            {
                DxfData = new DXFN12List(reader, cbdxfData);
            }
            if (cbdxfAgg != 0)
                DxfAgg = new DXFN12List(reader, cbdxfAgg);
            if (cbdxfBorder != 0) DxfBorder = new DXFN12List(reader, cbdxfBorder);
            if (cbdxfHeaderBorder != 0) DxfHeaderBorder = new DXFN12List(reader, cbdxfHeaderBorder);
            if (cbdxfAggBorder != 0) DxfAggBorder = new DXFN12List(reader, cbdxfAggBorder);
            if (istnHeader != -1)
            {
                _stHeader = new XLUnicodeString();
                _stHeader.Read(reader);
            }
            if (istnData != -1)
            {
                this.stData = new XLUnicodeString();
                stData.Read(reader);
            }
            if (istnAgg != -1)
            {
                stAgg = new XLUnicodeString();
                stAgg.Read(reader);
            }
        }

        public override void Write(BinaryWriter writer)
        {
            var cbdxfHeader = new DeferredIntWriter(writer);
            writer.Write(istnHeader);
            var cbdxfData = new DeferredIntWriter(writer);
            writer.Write(istnData);
            var cbdxfAgg = new DeferredIntWriter(writer);
            writer.Write(istnAgg);
            var cbdxfBorder = new DeferredIntWriter(writer);
            var cbdxfHeaderBorder = new DeferredIntWriter(writer);
            var cbdxfAggBorder = new DeferredIntWriter(writer);
            using (cbdxfHeader.GetLength())
            {
                if (DxfHeader != null)
                {
                    DxfHeader.Write(writer);
                }
            }
            using (cbdxfData.GetLength())
                if (DxfData != null)
                {
                    DxfData.Write(writer);
                }
            using (cbdxfAgg.GetLength())
                if (DxfAgg != null)
                    DxfAgg.Write(writer);
            using (cbdxfBorder.GetLength())
                if (DxfBorder != null) DxfBorder.Write(writer);
            using (cbdxfHeaderBorder.GetLength())
                if (DxfHeaderBorder != null) DxfHeaderBorder.Write(writer);
            using (cbdxfAggBorder.GetLength())
                if (DxfAggBorder != null) DxfAggBorder.Write(writer);
            if (istnHeader != -1)
            {
                _stHeader.Write(writer);
            }
            if (istnData != -1)
            {
                stData.Write(writer);
            }
            if (istnAgg != -1)
            {
                stAgg.Write(writer);
            }
        }
    }
}